define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/user/index' + location.search,
                    add_url: 'chase/user/add',
                    edit_url: 'chase/user/edit',
                    del_url: 'chase/user/del',
                    multi_url: 'chase/user/multi',
                    table: 'users',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'openid', title: __('Openid'), operate: false},
                        {field: 'merchant_id', title: __('商户ID')},
                        {field: 'merchant.title', title: __('商户名称'), operate: false},
                        {field: 'head', title: __('Head'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'nickname', title: __('Nickname'), operate: 'like'},
                        {field: 'phone', title: __('Phone')},
                        {field: 'sex', title: __('Sex'), searchList: {"female":__('Sex female'),"male":__('Sex male'),"unknown":__('Sex unknown')}, formatter: Table.api.formatter.label},
                        {field: 'is_vip', title: __('Is_vip'), searchList: {"no":__('Is_vip no'),"yes":__('Is_vip yes')}, formatter: Table.api.formatter.label},
                        {field: 'is_freeze', title: __('Is_freeze'), searchList: {"no":__('Is_freeze no'),"yes":__('Is_freeze yes')}, formatter: Table.api.formatter.toggle, yes: 'yes', no: 'no'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'expense_money', title: __('Expense_money'), operate:'BETWEEN'},
                        {field: 'recharge_money', title: __('Recharge_money'), operate:'BETWEEN'},
                        {field: 'user_coupon_count', title: __('优惠券数量'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                                return `<a href="javascript:;" class="btn btn-xs btn-default user-coupon" data-id="`+data.id+`" data-original-title="查看用户优惠券数量">`+data.user_coupon_count+`</a>`
                        }},
                        {field: 'vip', title: __('Vip'), operate:'BETWEEN'},
                        {field: 'ontime', title: __('Ontime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'description', title: __('Description')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            return `<a href="javascript:;" class="btn btn-xs btn-success btn-editone" data-toggle="tooltip" title="编辑" data-table-id="table" data-field-index="14" data-row-index="0" data-button-index="1"><i class="fa fa-pencil"></i></a>
                             <a href="javascript:;" data-id="`+data.id+`" class="btn btn-xs btn-success user-order" >消费记录</a>
                             <a href="javascript:;" data-id="`+data.id+`" class="btn btn-xs btn-success user-balance-log" >充值</a>
                             <a href="javascript:;" data-id="`+data.id+`" class="btn btn-xs btn-success user-grade">会员变更</a>`
                        }}
                    ]
                ]
            });

            //消费记录
            $('#table').on('click', '.user-order', function () {
                var id = $(this).data('id');
                Fast.api.open('/shijiu.php/chase/order/user?user_id='+id, '消费记录',{"area":['1280px', '720px']})
            })

            $('#table').on('click', '.user-coupon', function () {
                var data = $(this).data();
                console.log(data.id)
                Fast.api.open('/shijiu.php/chase/user_coupon/coupon?user_id='+data.id, '用户优惠券列表',{"area":['1280px', '720px']})
            })

            //充值
            table.on('click', '.user-balance-log', function () {
                var data = $(this).data();
                Fast.api.open('/shijiu.php/chase/user_balance_log/add?user_id='+data.id, '用户充值')
            })

            //变更会员等级
            table.on('click', '.user-grade', function () {
                var data = $(this).data();
                Fast.api.open('/shijiu.php/chase/user_grade/add?user_id='+data.id, '会员等级变更')
            })


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'chase/user/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'chase/user/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'chase/user/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});